﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;

namespace TestProject
{
    public partial class NewCharac : Form
    {
        private PhenotypeTable phen_t;
        private List<GenomePhenotypeLink> genome_links = new List<GenomePhenotypeLink>();
        private List<NaturalSelectionLink> natsel_links = new List<NaturalSelectionLink>();
        private Dictionary<string, ReproductionLink> repr_links = new Dictionary<string, ReproductionLink>();
        public NewCharac(PhenotypeTable t, List<GenomePhenotypeLink> list, List<NaturalSelectionLink> list1, Dictionary<string,ReproductionLink> list2)
        {
            InitializeComponent();
            phen_t = t;
            genome_links = list;
            natsel_links = list1;
            repr_links = list2;
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(phen_t.path));
            foreach (XmlNode node in doc.GetElementsByTagName("characteristic"))
            {
                if (node.Attributes[1].Value == name_box.Text)
                {
                    MessageBox.Show("Characteristic with this name already exists.\nPlease, choose another name.");
                    return;
                }
            }
            int id = Functions.set_id(phen_t);
            MyXmlDocument doc1;
            foreach (GenomePhenotypeLink link in genome_links)
            {
                doc1 = new MyXmlDocument();
                doc1.LoadXml(System.IO.File.ReadAllText(link.path));
                if (phen_t.characteristics_amount != Convert.ToInt32(doc1.DocumentElement.Attributes[4].Value) - 1)
                {
                SetLink:
                    SetGenPhenLink gen_phen_link = new SetGenPhenLink(link, degree_box.Value, id, name_box.Text);
                    if (gen_phen_link.ShowDialog(this) != DialogResult.OK)
                    {
                        MessageBox.Show("Unable to add characteristic without a link.\nPlease, create it.");
                        goto SetLink;
                    }
                }
            }            
            foreach (NaturalSelectionLink link in natsel_links)
            {
                doc1 = new MyXmlDocument();
                doc1.LoadXml(System.IO.File.ReadAllText(link.path));
                doc1.Increment(doc1.DocumentElement.Attributes[3]);
                if (doc1.GetElementsByTagName("characteristic_names")[0].InnerText.Contains(" "))
                {
                    doc1.GetElementsByTagName("characteristic_names")[0].InnerText += " " + name_box.Text;
                    doc1.GetElementsByTagName("characteristic_value_diapasons")[0].InnerText += " " + degree_box.Value.ToString();
                }
                else
                {
                    doc1.GetElementsByTagName("characteristic_names")[0].InnerText+= name_box.Text;
                    doc1.GetElementsByTagName("characteristic_value_diapasons")[0].InnerText += degree_box.Value.ToString();
                }
                string[] inner1 = doc1.GetElementsByTagName("characteristic_sets")[0].InnerText.Split(new char[] { '\t', '\n', '\r', ';' }, StringSplitOptions.RemoveEmptyEntries);
                string[] inner2 = doc1.GetElementsByTagName("characteristic_sets")[1].InnerText.Split(new char[] { '\t', '\n', '\r', ';' }, StringSplitOptions.RemoveEmptyEntries);
                doc1.GetElementsByTagName("characteristic_sets")[0].InnerText = null;
                doc1.GetElementsByTagName("characteristic_sets")[1].InnerText = null;
                for (int i = 0; i <= degree_box.Value; i++)
                {
                    if (inner1 != null)
                    {
                        for (int j = 0; j < inner1.Count(); j++)
                        {
                            doc1.GetElementsByTagName("characteristic_sets")[0].InnerText +=" "+ inner1[j] + " " + i.ToString() +";";
                        }
                    }
                    else
                    {
                        doc1.GetElementsByTagName("characteristic_sets")[0].InnerText += " "+i.ToString() + ";";
                    }
                    if (inner2 != null)
                    {
                        for (int j = 0; j < inner2.Count(); j++)
                        {
                            doc1.GetElementsByTagName("characteristic_sets")[1].InnerText +=" "+ inner2[j] + " " + i.ToString() +";";
                        }
                    }
                }
                doc1.Save(link.path);
            }
            foreach (ReproductionLink link in repr_links.Values)
            {
                if (link.path_to_phenotype_table == phen_t.path)
                {
                    doc1 = new MyXmlDocument();
                    doc1.LoadXml(System.IO.File.ReadAllText(link.path));
                    doc1.Increment(doc1.DocumentElement.Attributes[4]);
                    foreach (XmlNode node in doc.GetElementsByTagName("characteristic_sets"))
                    {
                        if (doc1.GetElementsByTagName("characteristic_names")[0].InnerText.Contains(" "))
                        {
                            doc1.GetElementsByTagName("characteristic_names")[0].InnerText += " " + name_box.Text;
                            doc1.GetElementsByTagName("characteristic_value_diapasons")[0].InnerText += " " + degree_box.Value.ToString();
                        }
                        else
                        {
                            doc1.GetElementsByTagName("characteristic_names")[0].InnerText += name_box.Text;
                            doc1.GetElementsByTagName("characteristic_value_diapasons")[0].InnerText += degree_box.Value.ToString();
                        }
                        string[] inner1 = node.InnerText.Split(new char[] { '\t', '\n', '\r', ';' }, StringSplitOptions.RemoveEmptyEntries);
                        node.InnerText = null;
                        for (int i = 0; i <= degree_box.Value; i++)
                        {
                            if (inner1 != null)
                            {
                                for (int j = 0; j < inner1.Count(); j++)
                                {
                                    node.InnerText += " " + inner1[j] + " " + i.ToString() + ";";
                                }
                            }
                            else
                            {
                                node.InnerText += " " + i.ToString() + ";";
                            }
                        }
                    }
                    doc1.Save(link.path);
                }
            }
            XmlElement el = doc.CreateElement("characteristic");
            el.SetAttribute("Id", id.ToString());
            el.SetAttribute("Name", name_box.Text);
            el.SetAttribute("MaxDegree", degree_box.Value.ToString());
            doc.DocumentElement.ChildNodes[0].AppendChild(el);
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.Save(phen_t.path);
            this.Close();
        }       
    }
}
